<?php

/**
 * newsletter actions.
 *
 * @package    sf_sandbox
 * @subpackage newsletter
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 2288 2006-10-02 15:22:13Z fabien $
 */
class newsletterActions extends autonewsletterActions
{
	public function executeView()
	{
		if($this->getRequestParameter('id'))
		{
			$newsletter = NewsletterPeer::retrieveByPk($this->getRequestParameter('id'));
			$this->forward404Unless($newsletter);
		}else 
		{
			$c = new Criteria();
            $c->addDescendingOrderByColumn(NewsletterPeer::UPDATED_AT);
            $c->add(NewsletterPeer::STATUS_ID, MyConstants::STATUS_APPROVED);
            $newsletter = NewsletterPeer::doSelectOne($c);
            if(!$newsletter)
            {
            	return $this->body = null;
            }
		}
        
        $newsIds = json_decode($newsletter->getItemsJson(), true);
        $arr = array();
        for( $i = 0; $i< count($newsIds); $i++)
        {
            $arr[] = Newsletter::newsletterFactory($newsIds[$i][0], $newsIds[$i][1], $newsletter->getLng());
        }
        sfLoader::loadHelpers('Partial');
        $newsList = get_partial('newsletter/mail_body'
                                , array('newsObjs' => $arr
                                		, 'header' => $newsletter->getHeader()));
        $this->body = $newsList;
	}
	
	protected function updateNewsletterFromRequest()
    {
    	parent::updateNewsletterFromRequest();
    	foreach($this->getRequestParameter('newsletter_items') as $newsId)
    	{
    		$newsItems[] = array(MyConstants::TYPE_NEWS, $newsId);
    	}
        $this->newsletter->setItemsJson(json_encode($newsItems));
        $this->newsletter->setLng($this->getRequestParameter('lng'));
    }

    public function executeIndex()
    {
        return $this->forward('newsletter', 'view');
    }

    public function executeSend()
    {
    	/* @var $newsletter Newsletter */
    	if( $this->getRequest()->getMethod() == sfRequest::POST )
        {
	        $newsletter = NewsletterPeer::retrieveByPk($this->getRequestParameter('id'));
	        if(!$newsletter)
	        {
	        	return $this->redirect('newsletter/list');
	        }
	        $newsIds = json_decode($newsletter->getItemsJson(), true);
	        $arr = array();
	        for( $i = 0; $i< count($newsIds); $i++)
	        {
	        	$arr[] = Newsletter::newsletterFactory($newsIds[$i][0], $newsIds[$i][1], $newsletter->getLng());
	        }
	        sfLoader::loadHelpers('Partial');
	        $newsList = get_partial('newsletter/mail_body'
	                                , array('newsObjs' => $arr
	                                		, 'header' => $newsletter->getHeader()));
	        $body = $newsList;
	
	        $c = new Criteria();
	        $c->add(SubscriberPeer::STATUS_ID, MyConstants::STATUS_APPROVED);
	        $c->add(SubscriberPeer::LNG, $newsletter->getLng());
	        $recipients = SubscriberPeer::doSelect($c);
	        $arRecipients = array();
	        
	        $webRequest = new sfWebRequest();
	      	$host = $webRequest->getHost();
	        foreach( $recipients as $recipient )
	        {
	        	MailTask::createNew(get_user_id()
	                           , MailTask::PRIORITY_NORMAL
	                           , $host
	                           , $recipient->getEmail()
	                           , $newsletter->getTitle()
	                           , $body
	                           , sfConfig::get('app_webmaster_mail')
	                           , sfConfig::get('app_webmaster_mail')
	                           );
	        }
	        $newsletter->setStatusId(MyConstants::STATUS_COMPLETED);
	        $newsletter->save();
	        $this->redirect('newsletter/list');
        }else 
        {
        	$this->newsletter = NewsletterPeer::retrieveByPk($this->getRequestParameter('id'));
        	$this->forward404Unless($this->newsletter);
        }
    }
    
	public function executeEnable()
    {
        $id = (int)$this->getRequestParameter('id');
        $obj = NewsletterPeer::retrieveByPk($id);
        $this->forward404Unless($obj);

        $obj->setStatusId(MyConstants::STATUS_APPROVED);
        $obj->save();
        $this->redirect('newsletter/list?id=' . $id);
    }
    public function executeDisable()
    {
        $id = (int)$this->getRequestParameter('id');
        $obj = NewsletterPeer::retrieveByPk($id);
        $this->forward404Unless($obj);

        $obj->setStatusId(MyConstants::STATUS_DELETED);
        $obj->save();
        $this->redirect('newsletter/list?id=' . $id);
    }
    protected function getNewsletterOrCreate($id = 'id')
  	{
  		/*@var $newsletter Newsletter */
  		$newsletter = parent::getNewsletterOrCreate($id);
  		if( $newsletter->getStatusId() < 1 )
  		{
  			$newsletter->setStatusId(MyConstants::STATUS_NEW);
  		}
    	return $newsletter;
  	}
    

}

